﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace LeafTransmission.Utils
{
    internal class AsyncUDPClientEventArgs
    {
        private UdpClient _udpWorkClient;
        private IPEndPoint _ipEndPoint;
        private byte[]? _bufferData;
        // 注意:当数据操作成功时,此参数为true,并且_bufferData不为null
        private bool _success;
        // 若发送失败时,此字段会包含错误信息
        private string? _executeMessage;


        // 触发此事件的udpClient实例
        public UdpClient WorkClient { get { return _udpWorkClient; } }
        // 端口信息
        public IPEndPoint Remote { get { return _ipEndPoint; } }
        // 原始byte数据
        public byte[]? BufferData { get { return _bufferData; } }
        /*
         * 只是对BufferData进行了默认封装
         * 根据定义好的字符编码转换为string类型的数据
         * 只是为了操作字方便,故再此处提供两种数据
         */
        public string? Message
        {
            get
            {
                return _bufferData != null ? Encoding.GetString(_bufferData, 0, _bufferData.Length) : null;
            }
        }
        // 默认字符编码
        public Encoding Encoding { get; set; } = Encoding.Unicode;

        // 数据是否发送成功
        public bool Success { get { return _success; } }

        public string? ExecuteMessage
        {
            get { return _executeMessage != null ? _executeMessage : null; }
        }
        public AsyncUDPClientEventArgs(UdpClient workClient, IPEndPoint ipEndPoint,bool success)
        {
            _udpWorkClient = workClient;
            _ipEndPoint = ipEndPoint;
            _success = success;
        }

        public AsyncUDPClientEventArgs(UdpClient workClient, IPEndPoint ipEndPoint, byte[] bufferData)
        {
            _udpWorkClient = workClient;
            _ipEndPoint = ipEndPoint;
            _bufferData = bufferData;
        }

        public AsyncUDPClientEventArgs(UdpClient workClient, IPEndPoint ipEndPoint, byte[] bufferData, bool success, string executeMessage)
        {
            _udpWorkClient = workClient;
            _ipEndPoint = ipEndPoint;
            _bufferData = bufferData;
            _success = success;
            _executeMessage = executeMessage;
        }

    }
}
